package ro.utcn.tp.taskManagement.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Config {

	// Keys for hibernate
	public static final String HIBERNATE_CONNECTION_URL_KEY = "hibernate.connection.url";

	public static final String HIBERNATE_USERNAME_KEY = "hibernate.connection.username";

	public static final String HIBERNATE_PASSWORD_KEY = "hibernate.connection.password";

	public static final String HIBERNATE_DRIVER_KEY = "hibernate.connection.driver_class";

	public static final String HIBERNATE_DIALECT_KEY = "hibernate.dialect";

	protected Properties prop = new Properties();

	// Other configurations
	public static final String CONFIG_FILE = "config.properties";

	private static Config instance;

	/**
	 * Class constructor
	 * 
	 * @throws ConfigException
	 */
	public Config() throws Exception {
		load();
	}

	protected void load() throws Exception {
		URL url = Config.class.getClassLoader().getResource(CONFIG_FILE);
		if (url == null) {
			throw new Exception("Cannot find " + CONFIG_FILE
					+ " file! Check your CLASSPATH!");
		} else {
			try {
				InputStream in = url.openStream();
				prop.load(in);
				in.close();
			} catch (IOException e) {
				throw new Exception(e.getMessage(), e);
			}
		}
	}

	public String getProperty(String name) throws Exception {
		String s = prop.getProperty(name);
		if ((s == null) || (s.length() == 0)) {
			return "";
		}
		return s;
	}

	public Map getProperties(String prefix) {
		Map<String, String> map = new LinkedHashMap<String, String>();
		Set keys = prop.keySet();
		Iterator iter = keys.iterator();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			if (!key.startsWith(prefix)) {
				continue;
			}
			String value = (String) prop.get(key);
			String newKey = key;
			map.put(newKey, value);
		}
		return map;
	}

	/**
	 * @return Returns an instance of this class
	 * @throws ConfigException
	 */
	public static Config getInstance() throws Exception {
		if (instance == null) {
			instance = new Config();
		}
		return instance;
	}

	/**
	 * Save the property to the file CONFIG_FILE
	 * 
	 * @throws ConfigException
	 */
	public void saveToFile() throws Exception {
		URL url = Config.class.getClassLoader().getResource(CONFIG_FILE);
		if (url == null) {
			throw new Exception("Cannot find " + CONFIG_FILE
					+ " file! Check your CLASSPATH!");
		} else {
			try {
				String outFile = url.getFile();
				FileOutputStream out = new FileOutputStream(outFile);
				prop.store(out, null);
				out.close();
			} catch (IOException e) {
				throw new Exception(e.getMessage(), e);
			}
		}
	}

	/**
	 * Set the property prop key "key" and value "value"
	 * 
	 * @param key
	 *            The property key
	 * @param value
	 *            The property value
	 */
	public void setProperty(String key, String value) {
		prop.setProperty(key, value);
	}

}
